﻿using EFFC.Frame.Net.Base.Common;
using EFFC.Frame.Net.Base.Data;
using EFFC.Frame.Net.Base.Data.Base;
using EFFC.Frame.Net.Global;
using EFFC.Frame.Net.Module.HttpCall;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace XHRPMIS.Common
{
    public class DataCenterAPI : SimpleRestCallHelper
    {
        public DataCenterAPI(string url = "", string access_id = "", string access_token = "")
        {
            ServerUrl = string.IsNullOrEmpty(url) ? MyConfig.GetConfiguration("DataCenterAPI", "ServerUrl") : url;
            Access_ID = string.IsNullOrEmpty(access_id) ? MyConfig.GetConfiguration("DataCenterAPI", "Access_ID") : access_id;
            AccessToken = string.IsNullOrEmpty(access_token) ? MyConfig.GetConfiguration("DataCenterAPI", "Access_Token") : access_token;
        }
        /// <summary>
        /// 服务端连接
        /// </summary>
        public string ServerUrl
        {
            get; private set;
        }
        /// <summary>
        /// 访问的用户名
        /// </summary>
        public string Access_ID
        {
            get; private set;
        }
        /// <summary>
        /// 访问的用户密码
        /// </summary>
        public string AccessToken
        {
            get; private set;
        }
        protected string Send(string route, object data,string method)
        {
            FrameDLRObject content = FrameDLRObject.CreateInstance(data, EFFC.Frame.Net.Base.Constants.FrameDLRFlags.SensitiveCase);
            var result = base.Send(ServerUrl+ route, data, new
            {
                Authorization = $"Bearer {AccessToken}",
                Sub_System_id = Access_ID
            },method);
            GlobalCommon.Logger.WriteLog(EFFC.Frame.Net.Base.Constants.LoggerLevel.DEBUG, $"DataCenterAPI：\nRequest url:{method} {ServerUrl + route};Response Result={result}");
            return result;
        }
        public string Get(string route)
        {
            GlobalCommon.Logger.WriteLog(EFFC.Frame.Net.Base.Constants.LoggerLevel.DEBUG, $"DataCenterAPI:Get Start {ServerUrl + route};");
            var result = base.Get(ServerUrl+ route, new
            {
                Authorization = $"Bearer {AccessToken}",
                Sub_System_id = Access_ID
            });
            GlobalCommon.Logger.WriteLog(EFFC.Frame.Net.Base.Constants.LoggerLevel.DEBUG, $"DataCenterAPI：Get End {ServerUrl + route};Response Result={result}");
            return result;
        }

        /// <summary>
        /// 同步设备信息
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool SendDevice(IEnumerable<object> data,ref  string msg)
        {
            var str = Send($"/devices/update", new
            {
                data = data
            }, "patch");
            var result = FrameDLRObject.CreateInstance(str);
            if (result.error != null)
            {
                msg = str;
                return false;
            }
            else
            {
                if (result != null && result.result != null)
                {
                    if (result.result.code == "success")
                    {
                        if (IntStd.IsNotIntThen(result.result.invalid_total, -1) == 0)
                        {
                            return true;
                        }
                        else
                        {
                            msg = result.result.msg;
                            return false;
                        }
                    }
                    else
                    {
                        msg = result.result.msg;
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
        }
        /// <summary>
        /// 删除设备资料
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool DeleteDevice(IEnumerable<object> data, ref string msg)
        {
            var str = Send($"/devices/delete", new
            {
                data = data
            }, "patch");
            var result = FrameDLRObject.CreateInstance(str);
            if (result.error != null)
            {
                msg = result.error;
                return false;
            }
            else
            {
                if (result != null && result.result != null)
                {
                    if (result.result.code == "success")
                    {
                        return true;
                    }
                    else
                    {
                        msg = result.result.msg;
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
        }
    }
}
